package com.aliwo.procesors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tech.powerjob.common.utils.NetUtils;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.TaskResult;
import tech.powerjob.worker.core.processor.sdk.BroadcastProcessor;
import tech.powerjob.worker.log.OmsLogger;

import java.util.List;

/**
 * @author:xuyayuan
 * @date:2021/7/19 22:36
 * @description:TODO
 */
@Slf4j
@Component
public class BroadcastProcessorDemo implements BroadcastProcessor {
    @Override
    public ProcessResult preProcess(TaskContext context) throws Exception {
        System.out.println("===== BroadcastProcessorDemo#preProcess ======");
        //context.getOmsLogger().info("BroadcastProcessorDemo#preProcess, current host: {}", NetUtils.getLocalHost());
        System.out.println("xuyy    powerjob  ======= 广播执行   ip :"+ NetUtils.getLocalHost() +" port " +
                ": 27778    preProcess");
        if ("rootFailed".equals(context.getJobParams())) {
            return new ProcessResult(false, "console need failed");
        } else {
            return new ProcessResult(true);
        }
    }

    @Override
    public ProcessResult process(TaskContext taskContext) throws Exception {
        OmsLogger logger = taskContext.getOmsLogger();
        System.out.println("===== BroadcastProcessorDemo#process ======");
        System.out.println("xuyy    powerjob  ======= 广播执行   ip :"+ NetUtils.getLocalHost() +" port " +
                ": 27778    process");
        //logger.info("BroadcastProcessorDemo#process,=================================== current host: {}",
        //NetUtils.getLocalHost());
        long sleepTime = 1000;
        try {
            sleepTime = Long.parseLong(taskContext.getJobParams());
        } catch (Exception e) {
            logger.warn("[BroadcastProcessor] parse sleep time failed!", e);
        }
        Thread.sleep(Math.max(sleepTime, 1000));
        return new ProcessResult(true);
    }

    @Override
    public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) throws Exception {
        String jobparam = context.getJobParams();
        System.out.println("===== BroadcastProcessorDemo#postProcess ======");
        System.out.println("xuyy    powerjob  ======= 广播执行   ip :"+ NetUtils.getLocalHost() +" port " +
                ": 27778    postProcess");
        //context.getOmsLogger().info("BroadcastProcessorDemo#postProcess, " +
        //                "22222222222222222222222222222 current host: {}, taskResult: {}",
        //        NetUtils.getLocalHost(), taskResults);
        System.out.println("xuyy    powerjob  ======= 广播执行   ip :"+ NetUtils.getLocalHost() +" port " +
                ": 27778    taskResult : " + taskResults);
        return new ProcessResult(true, "success");
    }
}
